package com.sansecy.monitor.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sansecy.monitor.R;
import com.sansecy.monitor.see.LogBean;
import com.sansecy.monitor.utils.Utils;

import java.util.Locale;

/**
 * @Description 包含响应信息的视图
 * @Date 2019-08-08 10:53
 * @Created by caoyu
 */
public class ResponseView extends ScrollView implements View.OnClickListener {
    private TextView tvTookTime;
    private TextView tvContentType;
    private TextView tvStatusCode;
    private TextView tvMessage;
    private TextView tvGzipSize;
    private TextView tvTrafficSize;
    private TextView tvResponse;
    private TextView tvDecryptResponse;

    public ResponseView(Context context) {
        this(context, null);
    }

    public ResponseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(getContext()).inflate(R.layout.layout_response, this, true);
        init();
    }

    private void init() {
        tvTookTime = findViewById(R.id.tv_took_time);
        tvTookTime.setOnClickListener(this);
        tvContentType = findViewById(R.id.tv_content_type);
        tvContentType.setOnClickListener(this);
        tvStatusCode = findViewById(R.id.tv_status_code);
        tvStatusCode.setOnClickListener(this);
        tvMessage = findViewById(R.id.tv_message);
        tvMessage.setOnClickListener(this);
        tvGzipSize = findViewById(R.id.tv_gzip_size);
        tvGzipSize.setOnClickListener(this);
        tvTrafficSize = findViewById(R.id.tv_traffic_size);
        tvTrafficSize.setOnClickListener(this);
        tvResponse = findViewById(R.id.tv_response);
        tvResponse.setOnClickListener(this);
        tvDecryptResponse = findViewById(R.id.tv_decrypt_response);
        tvDecryptResponse.setOnClickListener(this);
    }

    public void bindData(LogBean logBean) {
        tvTookTime.setText(String.format(Locale.getDefault(), "%d ms", logBean.getTookTime()));
        tvContentType.setText(logBean.getResponseMediaType());
        tvGzipSize.setText(String.valueOf(logBean.getGzipSize()));
        tvMessage.setText(logBean.getMessage());
        tvTrafficSize.setText(logBean.getTrafficSize());
        tvStatusCode.setText(logBean.getStatusCode());
        tvResponse.setText(logBean.getResponseBody());
        tvDecryptResponse.setText(logBean.getDecryptResponse());
    }

    @Override
    public void onClick(View v) {
        Utils.copyToClipboard(v);
    }
}
